package com.onpositive.datacollection.editor;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.impl.DataCollection;
import com.onpositive.datamap.core.impl.DataObject;
import com.onpositive.semantic.model.api.realm.IRealm;

public class View extends CollectionView {

	public <T> View() {
		super("collection.dlf");
		setSelectionProvider("d");

	}
	public Object getProps(){
		DataCollection dataCollection = (DataCollection)DataCollectionAcess.collection();
		return dataCollection.getProperties(null);
	}
	
	
	public void save(){
		DataCollection dataCollection = (DataCollection)DataCollectionAcess.collection();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		try {
			DataOutputStream s = new DataOutputStream(byteArrayOutputStream);
			dataCollection.write(s);
			s.close();
			byte[] byteArray = byteArrayOutputStream.toByteArray();
			DataCollection c1=new DataCollection(byteArray);
			for (int a=0;a<dataCollection.getCount();a++){
				DataObject object = (DataObject) dataCollection.getObject(a);
				DataObject object2 = (DataObject) c1.getObject(a);
				IDataPropertyValue[] properties = object.getProperties();
				IDataPropertyValue[] properties2 = object2.getProperties();
				if (!Arrays.equals(properties, properties2)){
					for (int b=0;b<properties.length;b++){
						IDataPropertyValue m0=properties[b];
						IDataPropertyValue m1=properties2[b];
						if (!m0.equals(m1)){
							throw new IllegalStateException();
						}
					}
					
				}
				if (!object.getTextContentAsString().equals(object2.getTextContentAsString())){
					throw new IllegalStateException();
				}				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public IRealm<?> getData(){
		DataCollection dataCollection = (DataCollection)DataCollectionAcess.collection();
		if (dataCollection==null){
			return null;
		}
		return dataCollection.getRealm();		
	}
	@Override
	protected void doRefresh() {
		getBinding("Data").refresh();
	}
}